package lumis.portal.page.serialization;

import java.util.Collection;
import java.util.List;

import lumis.portal.page.PageConfig;
import lumis.portal.serialization.DeserializationContext;
import lumis.portal.serialization.operation.ConfigDependencyAnalyzer;
import lumis.portal.serialization.operation.ConfigForeignKey;

/**
 * Responsible for analyze foreign key dependencies of a page, 
 * in particular layout dependencies to service interface instances. 
 *
 * @version $Revision: 7370 $ $Date: 2007-06-12 13:50:49 -0300 (Tue, 12 Jun 2007) $
 * @since 4.0.11
 */
public class PageDependencyAnalyzer extends ConfigDependencyAnalyzer<PageConfig>
{
	public PageDependencyAnalyzer(DeserializationContext deserializationContext, List<ConfigForeignKey> configForeignKeys, PageConfig originalConfig)
	{
		super(deserializationContext, configForeignKeys, originalConfig);
	}

	@Override
	protected void analyzeForeignKeys()
	{
		super.analyzeForeignKeys();
		
		Collection<String> violatedForeignKeys = PageLayoutUtil.getViolatedForeignKeys(deserializationContext, originalConfig);
		
		if (!violatedForeignKeys.isEmpty())
		{
			dependencyKeys.addAll(violatedForeignKeys);
			persistableConfig.setLayout(null);
			hasViolation = true;
		}
	}
	
	@Override
	public boolean foreignKeysExists()
	{
		return super.foreignKeysExists() && PageLayoutUtil.getViolatedForeignKeys(deserializationContext, originalConfig).isEmpty();
	}

	@Override
	public PageConfig getMappedPersistableConfig()
	{
		PageConfig pageConfig = super.getMappedPersistableConfig();
		PageLayoutUtil.applyIdMappings(deserializationContext, pageConfig);
		return pageConfig;
	}
}
